<template>
    <div>
        <div>
            <el-button type="danger" size="small" :disabled="this.multipleSelection.length == 0" @click="deleteMany">批量删除</el-button>
            <el-input class="addPosInput" size="small" prefix-icon="el-icon-plus" placeholder="添加职位" v-model="pos.name"
                @keydown.enter.native="addPosition"></el-input>
            <el-button size="small" @click="addPosition" icon="el-icon-plus" type="primary">添加</el-button>
        </div>
        <div>
            <el-table class="posManaMain" :data="positions" size="small" border stripe style="width: 100%"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="50" align="center">
                </el-table-column>
                <el-table-column prop="id" label="编号" width="50" align="center">
                </el-table-column>
                <el-table-column prop="name" label="职位" width="120" align="center">
                </el-table-column>
                <el-table-column prop="createDate" label="创建时间" width="130" align="center">
                </el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%">
            <div>
                <el-form :model="updatePos" :rules="rules" ref="updatePos" label-width="30%" class="demo-ruleForm">
                    <el-form-item label="职位名称" prop="name">
                        <el-input v-model="updatePos.name"  style="width: 205px;" size="small"></el-input>
                    </el-form-item>
                    <el-form-item style="padding-top: 10px;padding-left: 100px;">
                        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                        <el-button size="small" type="primary" @click="doUpdate('updatePos')">确 定</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "PosMana",
    data() {
        return {
            pos: {
                name: ''
            },
            positions: [],
            dialogVisible: false,
            updatePos: {
                name: ''
            },
            multipleSelection: [],
            rules: {
                name: [
                    { required: true, message: '请输入职位名称', trigger: 'blur' }
                ],
            }
        }
    },
    mounted() {
        this.initPositions();
    },
    methods: {
        deleteMany() {
            this.$confirm('此操作将永久删除选中的 "' + this.multipleSelection.length + '"条 职位, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let ids = "?";
                this.multipleSelection.forEach(item => {
                    ids += 'ids=' + item.id + "&";
                });
                this.deleteRequest('/system/basic/pos/' + ids).then(resp => {
                    if (resp) {
                        this.initPositions();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        doUpdate(updatePos) {
            this.$refs[updatePos].validate((valid) => {
                if (valid) {
                    this.putRequest('/system/basic/pos/', this.updatePos).then(resp => {
                        if (resp) {
                            this.initPositions();
                            this.dialogVisible = false;
                        }
                    })
                } 
            });
        },
        addPosition() {
            if (this.pos.name) {
                this.postRequest("/system/basic/pos/", this.pos).then(resp => {
                    if (resp) {
                        this.initPositions();
                        this.pos.name = '';
                    }
                })
            } else {
                this.$message.warning("职位名称不能为空！");
            }
        },
        showEditView(index, data) {
            Object.assign(this.updatePos, data);
            this.dialogVisible = true;
            this.updatePos.createDate = '';
        },
        handleDelete(index, data) {
            this.$confirm('此操作将永久删除 "' + data.name + '" 职位, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
                    if (resp) {
                        this.initPositions();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        initPositions() {
            this.getRequest('/system/basic/pos/').then(resp => {
                if (resp) {
                    this.positions = resp;
                }
            })
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        }
    }
}

</script>


<style>
.addPosInput {
    margin-left: 80px;
    width: 230px;
    margin-right: 8px
}

.posManaMain {
    margin-top: 10px;
}
</style>